using UnityEngine;
using System.Collections.Generic;
using UndergroundSurvival.Rooms;

namespace UndergroundSurvival.Managers
{
    public class RoomPrefabManager : MonoBehaviour
    {
        public static RoomPrefabManager Instance { get; private set; }

        [System.Serializable]
        public class RoomPrefabData
        {
            public string roomName;
            public GameObject prefab;
            public Sprite icon;
            public string description;
            public List<ResourceCost> buildCosts;
            public List<ResourceCost> upgradeCosts;
        }

        [Header("Room Prefabs")]
        [SerializeField] private List<RoomPrefabData> roomPrefabs = new List<RoomPrefabData>();

        private Dictionary<string, RoomPrefabData> roomPrefabDict = new Dictionary<string, RoomPrefabData>();

        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
                InitializeRoomPrefabs();
            }
            else
            {
                Destroy(gameObject);
            }
        }

        private void InitializeRoomPrefabs()
        {
            foreach (var roomData in roomPrefabs)
            {
                if (!roomPrefabDict.ContainsKey(roomData.roomName))
                {
                    roomPrefabDict.Add(roomData.roomName, roomData);
                }
            }
        }

        public GameObject GetRoomPrefab(string roomName)
        {
            if (roomPrefabDict.TryGetValue(roomName, out RoomPrefabData roomData))
            {
                return roomData.prefab;
            }
            return null;
        }

        public RoomPrefabData GetRoomData(string roomName)
        {
            if (roomPrefabDict.TryGetValue(roomName, out RoomPrefabData roomData))
            {
                return roomData;
            }
            return null;
        }

        public List<RoomPrefabData> GetAllRoomData()
        {
            return roomPrefabs;
        }

        public bool HasEnoughResources(string roomName, bool isUpgrade = false)
        {
            if (!roomPrefabDict.TryGetValue(roomName, out RoomPrefabData roomData))
                return false;

            var costs = isUpgrade ? roomData.upgradeCosts : roomData.buildCosts;
            if (costs == null || costs.Count == 0)
                return true;

            foreach (var cost in costs)
            {
                if (!ResourceManager.Instance.HasEnoughResource(cost.resourceName, cost.amount))
                    return false;
            }
            return true;
        }

        public void ConsumeBuildResources(string roomName, bool isUpgrade = false)
        {
            if (!roomPrefabDict.TryGetValue(roomName, out RoomPrefabData roomData))
                return;

            var costs = isUpgrade ? roomData.upgradeCosts : roomData.buildCosts;
            if (costs != null && costs.Count > 0)
            {
                ResourceManager.Instance.ConsumeResources(costs);
            }
        }
    }
} 